import { _decorator, Node, Component, Button, Label, sys, director } from 'cc';
import RowFallClear_SettingPanel, { SettingPanelType } from './RowFallClear_SettingPanel';
import RowFallClear_GameManager, { GameState, GameType } from '../Manager/RowFallClear_GameManager';
import RowFallClear_CellFunctions from '../Core/RowFallClear_CellFunctions';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import { RowFallClear_GameData } from '../Data/RowFallClear_GameData';

const { ccclass, property } = _decorator;

@ccclass('RowFallClear_GamePlayPanel')
export default class RowFallClear_GamePlayPanel extends Component {
    public static Instance: RowFallClear_GamePlayPanel;

    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;

    @property(Button)
    Button_pause: Button | null = null;

    @property(Button)
    Button_L: Button | null = null;
    @property(Button)
    Button_M: Button | null = null;
    @property(Button)
    Button_R: Button | null = null;

    @property(Label)
    Label_score: Label | null = null;
    @property(Label)
    Label_highScore: Label | null = null;
    onLoad() {
        RowFallClear_GamePlayPanel.Instance = this;
    }
    start() {
        this.AddListener();

        this.Button_pause.node.on('click', this.click_pause, this);

        this.Button_L.node.on('click', () => {
            if (RowFallClear_GameData.IsCanOperate) {
                RowFallClear_GameData.IsCanOperate = false;
                RowFallClear_CellFunctions.MoveLefOrRight(-1);
            }
        }, this);
        this.Button_R.node.on('click', () => {
            if (RowFallClear_GameData.IsCanOperate) {
                RowFallClear_GameData.IsCanOperate = false;
                RowFallClear_CellFunctions.MoveLefOrRight(+1);
            }
        }, this);
        this.Button_M.node.on('click', () => {
            if (RowFallClear_GameData.IsCanOperate) {
                RowFallClear_GameData.IsCanOperate = false;
                RowFallClear_CellFunctions.MoveDownAllCell();
            }
        }, this);
    }

    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }

    On_Button_back() {
        director.loadScene("MainScene");

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        // TurkishTetris_GameManager.Instance.ReStart();

        // let currentSceneName = director.getScene().name;
        // director.loadScene(currentSceneName);

        SSS_AudioManager.PlayButtonAudio();
    }

    ShowOn() {
        this.node.active = true;
    }
    ShowOff() {
        this.node.active = false;
    }
    RefreshScore(_score: number) {
        this.Label_score.string = _score.toString();
        let highScore = sys.localStorage.getItem('highScore' + GameType[RowFallClear_GameManager.Instance.gameType]);
        if (highScore == null) {
            highScore = 0;
        }
        if (_score > highScore) {
            highScore = _score;
            sys.localStorage.setItem('highScore' + GameType[RowFallClear_GameManager.Instance.gameType], highScore);
        }
        this.Label_highScore.string = highScore;
    }
    click_pause(button) {
        RowFallClear_GameManager.Instance.gameState = GameState.Pause;
        RowFallClear_SettingPanel.Instance.ShowOn(SettingPanelType.pause);
    }
}